(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.product.createRejectController', createRejectController);
	createRejectController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'productService' ];
	function createRejectController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, productService) {
		var ctrl = this, productInfo = $scope.$stateParams.params, errorMessage = {};
		$scope.authorization = $scope.$stateParams.authorization;
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.initValidatorConfig = angular.copy(ctrl.validatorConfig);
		ctrl.validatorConfig = {};
		ctrl.product = {};
		
		ctrl.init = function() {
			var params = {
					taskId : productInfo.taskId
			};
			// init acct
			productService.initProfAcct(layoutConfig, $scope, ctrl);
			// init int
			//productService.initProfInt(layoutConfig, $scope, ctrl);
			//productService.initProfCostOfFundInt(layoutConfig, $scope, ctrl);
			// init pastdue
			productService.initProfPastdue(layoutConfig, $scope, ctrl);
			// init fee
			productService.initProfFee(layoutConfig, $scope, ctrl);
			// init doc
			productService.initEventDoccfg(layoutConfig, $scope, ctrl);
			// init limit facility
			//productService.initProfLimitFaci(layoutConfig, $scope, ctrl);
			//productService.initProfLimitInsFaci(layoutConfig, $scope, ctrl);
			//productService.initProfLimitPurFaci(layoutConfig, $scope, ctrl);
			// init general
			productService.initGeneral($scope, ctrl).then(function() {
				authService.createReject(params).then(function(response) {
					var initProduct = response.data.model;
					ctrl.action = "createReject";
					productService.initLayoutConfigAndModel($scope, ctrl, initProduct, ctrl.action, true).then(function() {
						// ctrl.product = initProduct; done in initLayoutConfig
						ctrl.product.taskId = response.data.task.taskId || productInfo.taskId;
					});
				});
			});
		};

		ctrl.createRejectSave = function() {
			productService.clearDirtyData(ctrl);
			return authService.createRejectSave(ctrl.product).then(saveSuccessful, saveFailed);
		};

		ctrl.createRejectSubmit = function() {
			productService.clearDirtyData(ctrl);
			return authService.createRejectSubmit(ctrl.product).then(submitSuccessful, submitFailed);
		};
		
		ctrl.createRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.product.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createRejectCancel(ctrl.product).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.product.list',
					'state' : 'cda.product.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.product, response.data.model);
			} else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.product.list',
					'state' : 'cda.product.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

	}
})(angular);
